package Z.dailyExercise.March;

import java.util.HashMap;
import java.util.HashSet;

public class _剑指Offer48最长不含重复字符的子字符串 {

    /**
     * 子串问题：滑动窗口
     * 发现有重复之后 把第一个重复的排除走就是该收集结果的时候
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        HashMap<Character,Integer> window = new HashMap<>();
        int left=0,right=0;
        int res = 0;

        while (right<s.length()){

            char in = s.charAt(right);
            right++;

            window.put(in,window.getOrDefault(in,0)+1);

            while (window.get(in)>1){
                char out = s.charAt(left);
                left++;
                window.put(out,window.getOrDefault(out,1)-1);
            }

            res = Math.max(res,(right-left));


        }
        return res;

    }

}
